<template>
  <div class="goodsadd">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品添加</span>
      </div>
      <!-- 商品名称 -->
      <el-form class="mainadd" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <!-- 商品分类 -->
        <el-form-item label="商品分类">
          <el-select v-model="form.category" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.cateName"
              :value="item.cateName"
            >
              {{ item.cateName }}</el-option
            >
          </el-select>
        </el-form-item>
        <!-- 商品价格 -->
        <el-form-item label="商品价格">
          <el-input-number
            v-model="form.price"
            :min="0"
            :max="100"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <!-- 商品图片 -->
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            :action="Api + '/goods/goods_img_upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 商品描述 -->
        <el-form-item label="商品描述">
          <el-input v-model="form.goodsDesc" type="textarea"></el-input>
        </el-form-item>
        <!-- 添加商品 -->
        <el-form-item label="">
          <el-button type="primary" @click="uploader"
            >添加商品</el-button
          ></el-form-item
        >
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { goodsCategories, goodsAdd } from '@/api/goods';
export default {
  data () {
    return {
      options: [],
      form: {
        name: '',
        category: "",
        price: '',
        imgUrl: "",
        goodsDesc: ""
      },
      Api: 'http://127.0.0.1:5000'
    }
  },
  methods: {
    async getData () {
      let res = await goodsCategories()
      this.options = res.data
    },
    //商品图片
    handleAvatarSuccess (res, file) {
      let { code, msg, imgUrl } = res
      if (code == 0) {
        this.form.imgUrl = this.Api + imgUrl
      }
      // console.log('res :>> ', res);
      // console.log('file :>> ', file);
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    //商品图片验证
    beforeAvatarUpload (file) {
      const isJPG = /[jpeg|png]/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    async uploader () {
      let { imgUrl } = this.form
      imgUrl = imgUrl.substr(imgUrl.lastIndexOf('/') + 1)
      let res = await goodsAdd({ ...this.form, imgUrl })
      let { code, msg } = res.data
      if (code == 0) {
        this.form = {
          name: '',
          category: "",
          price: '',
          imgUrl: "",
          goodsDesc: ""
        }
      }
    }
  },
  created () {
    this.getData();
  },
}
</script>

<style lng="less" scoped>
.mainadd {
  width: 500px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px solid;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>